package com.unis.db;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.CharArrayReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.sql.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.rowset.serial.SerialBlob;
import javax.sql.rowset.serial.SerialClob;

import org.dom4j.DocumentException;

import com.unis.utils.Config;


public class DBManager  {
	private static Pool pool;
	private static Map<Object,Connection> pc;
	private static Map<Object,PreparedStatement> pps;
	
	
	private static void CreateConnection() {
		try {
			String dbName = Config.getConfigValue("/db//dbName");
			String userName = Config.getConfigValue("/db//userName");
			String passWord = Config.getConfigValue("/db//passWord");
			String port = Config.getConfigValue("/db//port");
			String url = Config.getConfigValue("/db//url");
			String dbtype = Config.getConfigValue("/db//dbtype");

			pool = Pool.getInstance(dbName, userName, passWord, port, url, dbtype );
			pc = new HashMap<Object, Connection>();
			pps = new HashMap<Object, PreparedStatement>();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static Connection getCon(Object obj){
		
		if (pool == null) {
			 CreateConnection();
		}
		
		 Connection conn=pool.getConnection();
		 pc.put(obj, conn);
		return  conn;
		
	}

	public static void closeCon(Object obj) throws Exception{
		PreparedStatement pstm=pps.get(obj);
		Connection conn= pc.get(obj);
		if(pstm != null){
			pstm.close();
		}
		pool.freeConnection(conn);
		
	//	Thread.sleep(100);
		pps.remove(obj);
		pc.remove(obj);
		
	}

	/**
	 * 
	 * 执行�?���?��的查询语�?
	 * 
	 * 返回取得的结果集
	 */

	public static ResultSet executeQuery(String sql,Object obj) throws SQLException{
		ResultSet rs = null;
		Connection con = getCon(obj);
		PreparedStatement pstm=con.prepareStatement(sql); 
		pps.put(obj, pstm);
		try {
			rs = pstm.executeQuery(); 
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
		}
		
		return rs;
	}

	/**
	 * 
	 * 执行�?���?��的更新语�?
	 * 
	 * 执行成功则返回true
	 * 
	 */

	public static boolean executeUpdate(String sql,Object obj) throws SQLException{
		boolean v = false;
		Connection con = getCon(obj);
		PreparedStatement pstm=con.prepareStatement(sql); 
		pps.put(obj, pstm);
		try {
			v = pstm.executeUpdate() > 0 ? true : false;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			return v;
		}
	}
	
	public static boolean insertOrUpdata(String sql, List parameters, List parameterTypes, Object obj) {
		// TODO Auto-generated method stub
		Connection connection = getCon(obj);
		try {
			PreparedStatement insertStatement = connection .prepareStatement(sql);
			pps.put(obj, insertStatement);
			for (int i = 0; i < parameters.size(); i++) {
				if (Byte.class == parameterTypes.get(i)) {
					byte[] blob = (byte[]) parameters.get(i);
					InputStream sbs = new ByteArrayInputStream(blob); 
					insertStatement.setBinaryStream(i + 1, sbs);
				}
				if (Character.class == parameterTypes.get(i)) {
					char[] clob = (char[]) parameters.get(i);
					CharArrayReader reader = new CharArrayReader(clob);
					insertStatement.setCharacterStream(i+1, reader);
				}			
				if (Date.class == parameterTypes.get(i)) {
					if(parameters.get(i) != null){
						insertStatement.setDate(i + 1, (Date)parameters.get(i));
					}else{
						insertStatement.setDate(i + 1, null);
					}
				}			
				if (String.class == parameterTypes.get(i)) {
					if(parameters.get(i) != null){
						insertStatement.setString(i + 1, parameters.get(i) .toString());
					}else{
						insertStatement.setString(i + 1, null);
					}
				}
			}
			for (int i = 0; i < parameters.size(); i++) {
				if (Byte.class == parameterTypes.get(i)) {
					byte[] blob = (byte[]) parameters.get(i);
					InputStream sbs = new ByteArrayInputStream(blob); 
					insertStatement.setBinaryStream(i +1+parameters.size(), sbs);	
				}
				if (Character.class == parameterTypes.get(i)) {
					char[] clob = (char[]) parameters.get(i);
					CharArrayReader reader = new CharArrayReader(clob);
					insertStatement.setCharacterStream(i +1+parameters.size(), reader);
				}			
				if (Date.class == parameterTypes.get(i)) {
					if(parameters.get(i) != null){
						insertStatement.setDate(i +1+parameters.size(), (Date)parameters.get(i));
					}else{
						insertStatement.setDate(i +1+parameters.size(), null);
					}
				}			
				if (String.class == parameterTypes.get(i)) {
					if(parameters.get(i) != null){
						insertStatement.setString(i +1+parameters.size(), parameters.get(i) .toString());
					}else{
						insertStatement.setString(i +1+parameters.size(), null);
					}
				}
			}
			insertStatement.execute();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
}
